#include<bits/stdc++.h>
#define ll long long
#define L x<<1
#define R x<<1|1
#define mid (l+r>>1)
#define lc L,l,mid
#define rc R,mid+1,r
#define OK Ll<=l&&r<=Rr
#define Root 1,1,n
#define rep(x,y,z) for(int x=(y);x<=(z);x++)
#define per(x,y,z) for(int x=(y);x>=(z);x--)
#define repn(x) rep(x,1,n)
#define repm(x) rep(x,1,m)
#define pb push_back
#define e(x) for(int i=h[x],y=to[i];i;i=nxt[i],y=to[i])
#define E(x) for(auto y:p[x])
#define Pi pair<int,int>
#define ui unsigned ll
inline int read(){int s=0,w=1;char c=getchar();while(c<48||c>57) {if(c=='-') w=-1;c=getchar();}while(c>=48&&c<=57)s=(s<<1)+(s<<3)+c-48,c=getchar();return s*w;}
inline void pf(int x){if(x<0) putchar('-'),x=-x;if(x>9)pf(x/10);putchar(x%10+48);}
using namespace std;
const int N=5e4+10,M=7e6+5,inf=(1LL<<31)-1,mod=998244353;
const ll llf=1e18;
inline void add(int &a,int b){((a+=b)>=mod) and (a-=mod);}
inline int Add(int a,int b){return add(a,b),a;}
inline int mul(int a,int b){return 1LL*a*b%mod;}
inline void Mul(int &a,int b){a=mul(a,b);}
inline void red(int &a,int b){add(a,mod-b);}
inline int Red(int a,int b){return red(a,b),a;}
inline int qp(int a,int b){if(!b)return 1;int c=qp(a,b>>1);Mul(c,c);if(b&1)Mul(c,a);return c;}
inline int INV(int x){return qp(x,mod-2);}
int n,m,a[N],h[N],to[N],nxt[N],cnt,f[N],dfn[N],Id[N],Time,dep[N],top[N],siz[N],son[N];
inline void add_(int a,int b){
	to[++cnt]=b,nxt[cnt]=h[a],h[a]=cnt;
}
inline void dfs(int x,int fa){
	dep[x]=dep[fa]+1,siz[x]=1;
	e(x)dfs(y,x),siz[x]+=siz[y],son[x]=siz[y]>siz[son[x]]?y:son[x];
}
inline void Dfs(int x,int low){
	top[x]=low,dfn[x]=++Time,Id[Time]=x;
	if(son[x])Dfs(son[x],low);
	e(x)if(y^son[x])Dfs(y,y);
}
inline int lca(int x,int y){
	while(top[x]^top[y])dep[top[x]]>dep[top[y]]?x=f[top[x]]:y=f[top[y]];
	return dep[x]<dep[y]?x:y;
}
const int B=223;
#define le(x) (((x)-1)*B+1)
#define re(x) min((x)*B,n)
#define bel(x) (((x)-1)/B+1)
int xd[N<<2];
inline void build(int x,int l,int r){
	xd[x]=0;
	if(l^r)build(lc),build(rc);
}
inline void modify(int x,int l,int r,int p,int k){
	xd[x]+=k;
	if(l^r)p<=mid?modify(lc,p,k):modify(rc,p,k);
}
inline int query(int x,int l,int r,int Ll,int Rr){
	if(OK)return xd[x];
	if(Rr<=mid)return query(lc,Ll,Rr);
	if(Ll>mid)return query(rc,Ll,Rr);
	return query(lc,Ll,Rr)+query(rc,Ll,Rr);
}
inline int Kth(int x,int l,int r,int k){
	if(l==r)return l;
	return xd[L]>=k?Kth(lc,k):Kth(rc,k-xd[L]);
}
inline int pr(int x){
	if(x==1)return -1;
	int w=query(Root,1,x-1);
	if(!w)return -1;
	return Kth(Root,w);
}
inline int nx(int x){
	if(x==n)return -1;
	int w=query(Root,x+1,n);
	if(!w)return -1;
	return Kth(Root,xd[1]-w+1);
}
int s[N],tp,out[N];
struct node{
	int l,r,id;
};
bool vis[N];
inline void Clear(){
	while(tp)modify(Root,dfn[s[tp]],-1),vis[s[tp]]=0,tp--;
}
inline void insert(int x,int ty){
	if(vis[x])return;
	vis[x]=1,modify(Root,dfn[x],1);
	if(ty)s[++tp]=x;
}
inline bool cmp(node a,node b){
	return a.r<b.r;
}
inline void Insert(int x,int ty){
	int p=pr(dfn[x]),s=nx(dfn[x]);
	insert(x,ty);
	if(p!=-1)insert(lca(x,Id[p]),ty);
	if(s!=-1)insert(lca(x,Id[s]),ty);
}
inline int solve(int l,int r){
	Clear();
	rep(i,l,r)Insert(a[i],1);
	return xd[1];
}
inline void clear(){
	repn(i)vis[i]=0;
	tp=0,build(Root);
}
vector<node>p[N];
inline void Main(){
	n=read(),m=read();
	rep(i,2,n)f[i]=read(),add_(f[i],i);
	repn(i)a[i]=read();
	dfs(1,0),Dfs(1,1);
	repm(i){
		int l=read(),r=read();
		if(bel(l)==bel(r))out[i]=solve(l,r);
		else p[bel(l)].pb({l,r,i});
	}
	rep(i,1,bel(n)){
		sort(p[i].begin(),p[i].end(),cmp);
		int Rr=re(i);
		clear();
		for(auto nw:p[i]){
			Clear();
			while(Rr<nw.r)Rr++,Insert(a[Rr],0);
			rep(j,nw.l,re(i))Insert(a[j],1);
			out[nw.id]=xd[1];
		}
	}
	repm(i)cout <<out[i]<<"\n";
}
signed main(){
	freopen("escape.in","r",stdin);
	freopen("escape.out","w",stdout);
	int T=1;
	while(T--)Main();
	return 0;
}